/**
 * Created by Dalyn on 2017/3/17.
 * 充值编辑-弹框
 */

import React , {Component}from 'react';
import Relay from "react-relay";
import "./css/recharge.css"
import InputBox from "../../common/InputBox";
import Confirm from "../../common/Confirm";
import EnterpriseRechargeMutation from "./mutation/EnterpriseRechargeMutation"

import {_Modal, _Icon} from 'ANT_DE';
const Modal = _Modal,
       Icon = _Icon;

export default class RechargeSet extends React.Component {
    constructor(props){
        super(props);
        this.state = {
            displayflag:true,
            // is:true,
            priseOrder:"",
            amount:"",
            flowing:"",
            /*错误提示*/
            priseOrderError:"",
            amountError:"",
            flowingError:"",
        };
        this.style = {
            confirm: {
                width: '60px',
                height: '30px',
                backgroundColor: "#1b3249",
                fontSize: "18px",
                color: "#fff",
                marginTop: "26px",
                marginLeft: "20px",
                fontFamily: "Microsoft YaHei"
            },
            inputStyle: {
                width: 200,
                height: "22px",
                marginTop:"10px",
                marginLeft: 20
            },
        };
        this.isshow = 'none';
        this.isRelay=true
    }
    ///asdasdsadasd
    /*失去焦点*/
    bluron(type){
       /* this.setState({
            hint:""
        });*/
        switch (type){
            case "priseOrder":
                let priseOrderValue = this.refs._priseOrder.state.text;
                let reg1 = /^[1-9]\d{0,50}$/g;
                let reg2 = /^\s+$/g;
                if(reg1.test(priseOrderValue)){
                    this.setState({
                        priseOrderError:""
                    })
                }else if(reg2.test(priseOrderValue)){
                    this.isRelay=false;
                    this.setState({
                        priseOrderError:"企业ID不能包含空格"
                    })
                }else {
                    this.isRelay=false;
                    this.setState({
                        priseOrderError:"请正确填写企业ID"
                    })
                }
                return;
            case "amount":
                let amountValue = this.refs._amount.state.text;
                // let reg3 =/^([0-9]{1,10}(\.[0-9]{1,2})?|0\.[1-9]{1,2}|0\.0[1-9])$/g;
                /*let reg3 =/^(([1-9][0-9]{0,9})|[1-9]{1,10}(\.[0-9]{1,2})?|0\.[1-9]{1,2}|0\.0[1-9])$/g;*/
                let reg3 = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/g;
                let reg4 = /^\s+$/g;

                if(!reg3.test(amountValue)){
                    this.isRelay=false;
                    this.setState({
                        amountError:"请正确填写充值金额"
                    })
                    return;
                }else if(reg4.test(flowingValue)){
                    this.isRelay=false;
                    this.setState({
                        flowingError:"充值金额错误，不能包含空格"
                    })
                    return;
                }else if(parseFloat(amountValue) <= 0){
                    this.isRelay=false;
                    this.setState({
                        amountError:"请正确填写充值金额"
                    })
                    return;
                }
                this.setState({
                    amountError:""
                })
                return ;
            case "flowing":
                let flowingValue = this.refs._flowing.state.text;
                let reg5 = /^\w{1,50}$/g;
                let reg6 = /^\s+$/g;
                if(reg5.test(flowingValue)){
                    this.setState({
                        flowingError:""
                    })
                }else if(reg6.test(flowingValue)){
                    this.isRelay=false;
                    this.setState({
                        flowingError:"交易流水号错误，不能包含空格"
                    })
                }else {
                    this.isRelay=false;
                    this.setState({
                        flowingError:"请正确填写交易流水号"
                    })
                }
                return;
        }
    }
    /*获取焦点*/
    focuson(type){
        switch (type){
            case "priseOrder":
                this.isRelay=true;
                this.setState({
                    priseOrderError:"",
                    hint:""
                });
                return;
            case "amount":
                this.isRelay=true;
                this.setState({
                    amountError:"",
                    hint:""
                });
                return;
            case "flowing":
                this.isRelay=true;
                this.setState({
                    flowingError:"",
                    hint:""
                });
                return;
        }
    }
    //sd
    /*点击确认*/
    handleConfirm(flag){
        /**/
        this.bluron("priseOrder");
        this.bluron("amount");
        this.bluron("flowing");
        if(this.isRelay){
            Relay.Store.commitUpdate(
                new EnterpriseRechargeMutation({
                    accountRecordId:this.props.item.accountRecordId,
                    code:this.refs._priseOrder.state.text,
                    amount:this.refs._amount.state.text,
                    tranNo:this.refs._flowing.state.text,
                    createByName:sessionStorage.getItem('msUserName'),
                    createBy:sessionStorage.getItem('msUid')
                }),
                {
                    onFailure:()=>{
                        console.log("请求失败")
                    },
                    onSuccess:(response)=>{
                        let data = response.enterpriseRecharge.data;
                        if(data.errorCode==="0"){
                            /*请求成功-刷新数据，弹框隐藏*/
                            this.props.myRelay();
                            this.setState({
                                displayflag:!flag,
                                hint:""
                            });
                        }else {
                            if(data.errorCode == "200002"){
                                this.setState({
                                    priseOrderError:data.value,
                                    hint:""
                                });
                            }else if(data.errorCode == "200004" || data.errorCode == "200005"){
                                this.setState({
                                    flowingError:data.value,
                                    hint:""
                                });
                            }else if(data.errorCode == "200003" || data.errorCode == "200006"){
                                this.setState({
                                    amountError:data.value,
                                    hint:""
                                });
                            }else {
                                this.setState({
                                    hint:data.value
                                });
                            }
                        }
                    }

                }
            );
        }
        // console.log(this.props.arr_partyBEnid)
    }
    /*点击取消*/
    handleCancel(flag){
        this.setState({
            displayflag:!flag
        });
    }

    /*初始化数据*/
    initHit(){
        this.setState({
            flowingError:"",
            priseOrderError:"",
            amountError:"",
            hint:"",
            displayflag:false
        },()=>{
            this.refs._priseOrder.setState({
                text:""
            });
            this.refs._amount.setState({
                text:""
            });
            this.refs._flowing.setState({
                text:""
            });
        });



    }
    render(){
        return (
        <Modal
            className="modal_gre-title"
            visible={!this.state.displayflag}
            title="充值"
            onCancel={this.handleCancel.bind(this,this.state.displayflag)}
            onOk={this.handleConfirm.bind(this,this.state.displayflag)}
        >
            {/*// <div className="recharge_hint_bg" style={this.state.displayflag?{display:'none'}:{display:'block'}}>*/}
                <div className="clear">
                    <div className=" recharge_set_input">
                        <span className="recharge_set_span">*</span>
                        <InputBox style={this.style.inputStyle} className="addstaff_input_bro_no" titletext={'企业ID：'} space={10} defaultvalute="请复制附言中的内容" ref={'_priseOrder'}  maxlength={50} bluron={this.bluron.bind(this,"priseOrder")} focuson={this.focuson.bind(this,"priseOrder")}/>
                        <span className="recharge_set_hint1">{this.state.priseOrderError}</span>
                    </div>
                    <div className=" recharge_set_input">
                        <span className="recharge_set_span">*</span>
                        <InputBox style={this.style.inputStyle} titletext={'充值金额：'} className="addstaff_input_bro_no"  space={10} defaultvalute="支持十位数字以内" ref={'_amount'}  maxlength={11} bluron={this.bluron.bind(this,"amount")} focuson={this.focuson.bind(this,"amount")}/>
                        <span className="recharge_set_span1">元</span>
                        <span className="recharge_set_hint1">{this.state.amountError}</span>
                    </div>
                    <div className=" recharge_set_input">
                        <span className="recharge_set_span">*</span>
                        <InputBox style={this.style.inputStyle} className="addstaff_input_bro_no"  titletext={'交易流水号：'} space={10} defaultvalute="限50个字符以内" ref={'_flowing'}  maxlength={50} bluron={this.bluron.bind(this,"flowing")} focuson={this.focuson.bind(this,"flowing")}/>
                        <span className="recharge_set_hint1">{this.state.flowingError}</span>
                    </div>
                    <div className="recharge_set_upbtn">
                        <div className="recharge_set_hint0">{this.state.hint}</div>
                    </div>
                </div>
            {/*// </div>*/}
        </Modal>
        )
    }
}